package com.itheima.controller;

import com.itheima.common.CommonResult;
import com.itheima.dto.UserDto;
import com.itheima.service.UserService;
import com.itheima.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")

public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/sendMsg")
    public CommonResult sendMsg(@RequestBody UserDto userDto) {
        boolean res = userService.sendMsg(userDto.getPhone());
        if (!res) {
            return CommonResult.error("号码错误");
        }
        return CommonResult.success("手机验证码短信发送成功");
    }

    @PostMapping("/login")
    public CommonResult login(@RequestBody UserDto userDto) {
        UserVo res = userService.login(userDto);
        return CommonResult.success("登录成功", res);
    }
    @PostMapping("/loginout")
    public CommonResult loginout(HttpServletRequest request){
        String token =request.getHeader("Authorization");
        userService.clearToken(token);
        return CommonResult.success("退出登录成功");
    }
}
